package com.woniuxy.consumerfeign.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import feign.Logger;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: yangtao
 * @date 2021/8/3 14:41
 * @Version 1.0
 */
@Configuration
public class RpcConfiguration {
    //负载均衡策略，因为openfeign内置ribbon，而且是通过ribbon作为负载均衡器
    //修改openfeign的负载均衡策略就是修改ribbon的负载均衡策略
    @Bean
    public IRule rule(){
        return new RandomRule();
    }
    //设置feign的日志级别，不同的级别显示不同的feign调用信息，默认是none不会显示任何信息
    @Bean
    public Logger.Level level(){
        return Logger.Level.FULL;
    }

    /**
     *此配置是为了服务监控而配置，与服务容错本身无关，springcloud升级后的坑
     *ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream"，
     *只要在自己的项目里配置上下面的servlet就可以了
     *否则，Unable to connect to Command Metric Stream 404
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
